package view;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.aop.windows.TransactionalDialog;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.layout.HorizontalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.arena.widgets.Label;

import ApplicationModels.AgregarCompromisoAppModel;
import ApplicationModels.CompromisosAppModel;
import Domain.Compromiso;

@SuppressWarnings("serial")
public class CompromisosWindow extends TransactionalDialog<CompromisosAppModel> {

	public CompromisosWindow(WindowOwner owner, CompromisosAppModel model) {
		super(owner, model);
	}

	@Override
	protected void createFormPanel(Panel mainPanel) {
		this.setTitle("Compromisos del empleado");
		Panel vista = new Panel(mainPanel);
		vista.setLayout(new ColumnLayout(2));
		// new Label(mainPanel).setText("nombre del empleado: ");
		new Label(mainPanel).bindValueToProperty("empleadoSeleccionado");
		this.createResultsGrid(mainPanel);
	}

	private void createResultsGrid(Panel mainPanel) {
		Table<Compromiso> table = new Table<Compromiso>(mainPanel,
				Compromiso.class);
		table.setHeigth(200);
		table.setWidth(450);

		table.bindItemsToProperty("compromisos");
		
		//FALTARIA PONER EL ADAPTER
//		.setAdapter(
//				new PropertyAdapter(Empleado.class,
//						"empleado.descripcionEntera"));
		table.bindValueToProperty("compromisoSeleccionado");

		
		this.describeResultsGrid(table);
	}

	private void describeResultsGrid(Table<Compromiso> table) {
		new Column<Compromiso>(table).setTitle("Empleado")
				.bindContentsToProperty("empleado.nombreYApellido");

		new Column<Compromiso>(table).setTitle("Fecha").bindContentsToProperty(
				"localDate");
	}

	// ACTIONS

	public void abrirACWindow() {
		this.openDialog(new AgregarCompromisoWindow(this, new AgregarCompromisoAppModel(this.getModelObject().getTienda())));
	}

	protected void openDialog(Dialog<?> dialog) {
		dialog.onAccept(new MessageSend(this.getModelObject(),
				"agregarCompromiso"));
		dialog.open();
	}

	public void cancelar() {
		this.close();
	}

	@Override
	protected void addActions(Panel panel) {
		Panel panelBotones = new Panel(panel);
		panelBotones.setLayout(new HorizontalLayout());

		new Button(panelBotones).setCaption("agregar").onClick(
				new MessageSend(this, "abrirACWindow"));

		// falta saber que compromiso seleccionó

		new Button(panelBotones).setCaption("eliminar").onClick(
				new MessageSend(this.getModelObject(), "eliminarCompromiso"));

		new Button(panelBotones).setCaption("cerrar").onClick(
				new MessageSend(this, "cancelar"));

	}

}
